package com.amit.services;

import com.amit.entities.LectureEntity;
import com.amit.entities.StudentEntity;
import com.amit.repositories.LectureRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: AD
 * Date: 08/01/13
 * Time: 09:39
 */

@SuppressWarnings({"SpringJavaAutowiringInspection", "UnusedDeclaration"})
@Service
public final class LectureServiceImpl implements LectureService {

    @Autowired
    private LectureRepository repository;

    @Autowired
    private StudentService studentService;

    public LectureServiceImpl() {

    }

    @Override
    @Transactional
    public Long addLecture(String name) {
        return repository.save(new LectureEntity(null, null, name)).getId();
    }

    @Override
    @Transactional
    public void removeLecture(Long lectureId) {
        repository.delete(lectureId);
    }

    @Override
    @Transactional
    public void addStudentToLecture(Long studentId, Long lectureId) {
        LectureEntity lectureEntity = repository.findOne(lectureId);
        lectureEntity.addStudent(studentService.readStudent(studentId));
        repository.save(lectureEntity);
    }

    @Override
    @Transactional
    public void removeStudentFromLecture(Long studentId, Long lectureId) {
        LectureEntity lectureEntity = repository.findOne(lectureId);
        lectureEntity.removeStudent(studentService.readStudent(studentId));
        repository.save(lectureEntity);
    }

    @Override
    @Transactional
    public List<LectureEntity> readAllLectures() {
        return repository.findAll();
    }

    @Override
    @Transactional
    public LectureEntity readLecture(Long id) {
        return repository.findOne(id);
    }
}
